import vue from '@vitejs/plugin-vue'
import { fileURLToPath, URL } from 'node:url'
import { defineConfig} from 'vite'
import { viteMockServe } from 'vite-plugin-mock'; 
/* element按需引入 */
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'

import viteCompression from 'vite-plugin-compression';

export default defineConfig(({command })=>{
  return {
    plugins: [
      vue(),
      viteMockServe ({
        mockPath:'mock',
        localEnabled: command === 'dev' //只在开发模式下启用
      }),
      AutoImport({
        resolvers:[ElementPlusResolver()] //element按需引入
      }),
      Components({
        resolvers:[ElementPlusResolver()] //element按需引入
      }),
      /* gzip静态资源压缩 */
      [viteCompression({
        verbose: true, //是否在控制台输出压缩结果
        disable:false, //不禁用压缩
        threshold: 10240, // 压缩前最小文件大小
        algorithm:'gzip', //文件类型
        ext:'.gz' //压缩算法
      })],
      

    ],
    resolve: {
      /* alias: 路径别名替换 */
      alias: {
        '@': fileURLToPath(new URL('./src', import.meta.url)),
      }
    },
    build: {
      chunkSizeWarningLimit: 1000,
      sourcemap: false, // 是否生产map文件，打包时不用
      cssCodeSplit: true,
      /* 去除调试代码 */
      minify: 'terser',
      terserOptions: {
        compress: {
          drop_console: true,  // 打包时去除console.log
          drop_debugger: true  // 打包时去除debugger
        },
      },
      /* 配置文件名 */
      rollupOptions: {
        output: {
          chunkFileNames: 'static/js/[name]-[hash].js',
          entryFileNames: 'static/js/[name]-[hash].js',
          assetsFileNames: 'static/[ext]/[name]-[hash].[ext]',
          compact: true //压缩代码，删除换行符等
        }
      },

    },

    server: {
      port: 3000
    }

    
  }
}

)
